<div class="row animate fadeIn">
    <form class="form-horizontal" id="cost_form">
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Add</i></h5>
        <div class="ibox-tools">    
             <button class="btn btn-primary btn-sm save-btn-tab" id="submit-uom">Save</button>
             <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
     </div>
                <div class="ibox-content">
                	<?php foreach ($items as $inv) { ?>
                    	<input  type="hidden" id="<?php echo $inv->item_id; ?>" value="<?php echo $inv->description; ?>">
                	<?php } ?>
                        <div class="form-group">
                            <label class="col-lg-3 control-label">Business Unit</label>
                            <div class="col-lg-4">
                                <input type="text" value="<?php echo $this->session->userdata('bu_name'); ?>" class="form-control" disabled name="">
                            </div>
                            <input type="hidden" name="company_id" value="2">
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 control-label">Item Number</label>
                            <div class="col-lg-4">
                                    <select  class="chosen-select" name="item_numbers" id="item_nums">
                                        <option value=""> -- </option>
                                        <?php foreach ($items as $inv) { ?>
                                            <option value="<?php echo $inv->item_num; ?>"><?php echo $inv->item_num; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                        </div>
                        <div class="form-group"><label class="col-lg-3 control-label">Description</label> 
		                    <div class="col-lg-4">
		                        <input type="text"  id="des" class="form-control" style="width:238px;" readonly>
		                    </div>
		                </div>
                        <div class="form-group"><label class="col-lg-3 control-label">Effective Date</label> 
		                    <div class="col-lg-4">
		                        <input type="search" placeholder="" name="effective_date" id="eff_date" class="form-control" style="width:238px;">
		                    </div>
		                </div>
                        <div class="form-group">
                            <label class="col-lg-3 control-label">Unit Cost</label>
                            <div class="col-lg-4">
                                <input type="text" style="text-align: right;" class="form-control" name="cost">
                            </div>
                        </div>
                    </form>
                </div>
</div>
<script>
    $(document).ready(function(){
            $('#cost_form').data('serialize',$('#cost_form').serialize());
	    	$('#item_nums').change(function(){
	            var item_id = '#'+ $('#item_nums').val();
	            var ee = $('.ibox-content').find(item_id).val();
	            $('#des').val(ee);
	        });
	        $(".chosen-select").chosen({width:"150px;"});
	        $('.i-checks').iCheck({
	                    checkboxClass: 'icheckbox_square-green',
	                    radioClass: 'iradio_square-green',
              });
	        $('#eff_date').datepicker({
                keyboardNavigation: false,
                forceParse: false,
                autoclose: true,
                format: "yyyy-mm-dd"
            });
            $.validator.setDefaults({ ignore: ":hidden:not(select)" })
	        $("#cost_form").validate({
                 rules: {
                     item_numbers: {
                          required: true
                     },
                     effective_date: {
                         required: true
                     },
                     cost: {
                         required: true,
                         number: true
                     }
                 },
                 submitHandler: function(form){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insert_cost'); ?>",
                        data: $('#cost_form').serialize(),
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                            if(data[0].error != 1){
                                notifymsg("success",". . .Inventory Cost Added Successfully");
                                close_add(); active_search();
                             }else{
                              notifymsg("danger",data[0].msg);
                             }
                           // if(data == "true"){
                           //      notifymsg("success",". . .Inventory Cost Added Successfully");
                           //      close_add(); active_search();
                           // }else{
                           //  notifymsg("danger","Error, Creation Failed");
                           // }
                        },
                        complete: function() {

                        },
                        beforeSend: function(){
                           
                        }
                    });
                 }
             });
            
            function alert_cancel(){
                if($('#cost_form').serialize()!=$('#cost_form').data('serialize')){
                // Form has changed!!!
                swal({
                                          title: "Are you sure?",
                                          text: "Your work will not be save",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                            swal("success");
                                              close_add(); active_search();
                                              notifyClass("clear","");
                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                }else{
                    close_add(); active_search();
                    notifymsg("hide","hide");
                }
            }


            $('#cancel_add').click(function(){
                alert_cancel();
            });

            $('#addX').click(function(){
                alert_cancel();        
            });

    	});
</script>    